/*
 * @Author: i@rysa.cn
 * @Date: 2021-04-16 15:25:14
 * @LastEditTime: 2022-07-12 14:14:30
 * @LastEditors: i@rysa.cn
 * @Description:
 * @FilePath: \go-utils\carbon\carbon.go
 */

package carbon

import "time"

type carbon struct {
	time.Time
}

func NewCarbon() *carbon {
	return &carbon{}
}

func (t *carbon) TodayOfBegin() time.Time {
	now := time.Now()
	return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
}
func (t *carbon) TodayOfEnd() time.Time {
	return t.TodayOfBegin().Add(time.Duration(86399 * time.Second))
}

func (t *carbon) IsToday(s time.Time) bool {
	now := time.Now()
	return now.Year() == s.Year() && now.Month() == s.Month() && now.Day() == s.Day()
}

func (t *carbon) IsYesterday(s time.Time) bool {
	yesterday := time.Now().AddDate(0, 0, -1)
	return yesterday.Year() == s.Year() && yesterday.Month() == s.Month() && yesterday.Day() == s.Day()
}

func (t *carbon) MonthOfBegin() time.Time {
	now := time.Now()
	return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
}
func (t *carbon) MonthOfEnd() time.Time {
	date := t.MonthOfBegin().AddDate(0, 1, -1)
	return time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, time.Local)
}

func (t *carbon) MonthOfBeginBy(s time.Time) time.Time {
	return time.Date(s.Year(), s.Month(), 1, 0, 0, 0, 0, time.Local)
}

func (t *carbon) MonthOfEndBy(s time.Time) time.Time {
	n := t.MonthOfBeginBy(s).AddDate(0, 1, -1)
	return time.Date(n.Year(), n.Month(), n.Day(), 23, 59, 59, 0, time.Local)
}

func (t *carbon) Age(s time.Time) int {
	return time.Now().Year() - s.Year()
}

func (t *carbon) LastMonthOfBegin() time.Time {
	now := time.Now()
	return time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, time.Local)
}
func (t *carbon) LastMonthOfEnd() time.Time {
	date := t.LastMonthOfBegin().AddDate(0, 1, -1)
	return time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, time.Local)
}

func (t *carbon) DayOfBeginBy(date time.Time) time.Time {
	return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
}

func (t *carbon) DayOfEndBy(date time.Time) time.Time {
	return time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, time.Local)
}
